package com.right.health.app.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;

import com.right.health.app.R;
import com.right.health.app.widget.wheelView.adapter.NumericWheelAdapter;
import com.right.health.app.widget.wheelView.listener.OnItemSelectedListener;
import com.right.health.app.widget.wheelView.view.WheelView;

import java.util.Calendar;

/*
 * Created by hopeliao on 2019-10-13
 *
 */
public class DateBottomPickerDialog extends BaseBottomSheetDialog implements View.OnClickListener {

    private WheelView mYearView;

    private WheelView mMonthView;
    private WheelView mDayView;


    private float[] mLastDateLimits;

    private int mLastChooseYear;
    private int mLastChooseMonth;
    private int mLastChooseDate;

    private OnDateChooseDelegate mDelegate;

    public DateBottomPickerDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.dialog_date_picker;
    }

    @Override
    protected void findViews() {
        super.findViews();
        mYearView = (WheelView) findViewById(R.id.year_view);
        mDayView = (WheelView) findViewById(R.id.day_view);
        mMonthView = (WheelView) findViewById(R.id.month_view);

        findViewById(R.id.view_placeholder).setOnClickListener(this);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        findViewById(R.id.tv_yes).setOnClickListener(this);

        Calendar calendar = Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR);
        int currMonth = calendar.get(Calendar.MONTH);
        int currDay = calendar.get(Calendar.DAY_OF_MONTH);


        mYearView.setAdapter(new NumericWheelAdapter(currYear, currYear + 10));
        mYearView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                mLastChooseYear = (int) mYearView.getAdapter().getItem(index);
            }
        });
        mLastChooseYear = currYear;
        mYearView.setCyclic(false);
        mYearView.setCurrentItem(0);

        mMonthView.setCyclic(false);
        mMonthView.setAdapter(new NumericWheelAdapter(1, 12));
        mMonthView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int chooseMonth = (int) mMonthView.getAdapter().getItem(index);
                refreshDay(chooseMonth - 1);

                mLastChooseMonth = chooseMonth;
            }
        });
        mLastChooseMonth = currMonth + 1;
        mMonthView.setCurrentItem(currMonth);

        mDayView.setCyclic(false);
        mDayView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                mLastChooseDate = (int) mDayView.getAdapter().getItem(index);
            }
        });
        mDayView.setCurrentItem(currDay - 1);
        refreshDay(currMonth);

    }

    private void refreshDay(int month) {
        if (mLastDateLimits == null) {
            mLastDateLimits = new float[2];
        }

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, month);
        int minNum = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        int maxNum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);


        if (mLastDateLimits[0] == minNum && mLastDateLimits[1] == maxNum) {
            return;
        }
        mLastDateLimits[0] = minNum;
        mLastDateLimits[1] = maxNum;

        mDayView.setAdapter(new NumericWheelAdapter(minNum, maxNum));
        mDayView.setCurrentItem(0);
        mLastChooseDate = 1;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.view_placeholder || v.getId() == R.id.tv_cancel) {
            dismiss();
        } else if (v.getId() == R.id.tv_yes) {
            if(mDelegate != null) {
                if(mLastChooseMonth == 0) {
                    mLastChooseMonth = 1;
                }
                mDelegate.onChoose(mTag, mLastChooseYear, mLastChooseMonth, mLastChooseDate);
            }
        }
    }

    public void setDelegate(OnDateChooseDelegate delegate) {
        this.mDelegate = delegate;
    }

    public interface OnDateChooseDelegate {
        void onChoose(Object tag, int year, int month, int date);
    }
}
